package com.helen.ui.statistics;

import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.helen.db.UserHelper;
import com.helen.entity.StatisticsEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.JsonUtil;
import com.helen.utils.MyLog;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import butterknife.BindView;

public class StatisticsActivity extends BaseActivity {
    @BindView(R.id.tv_jyf)
    TextView tvJyf;
    @BindView(R.id.tv_dzcp)
    TextView tvDzcp;
    @BindView(R.id.tv_sj)
    TextView tvSj;
    @BindView(R.id.tv_jj)
    TextView tvJj;
    @BindView(R.id.tv_lj)
    TextView tvLj;
    @BindView(R.id.tv_sp)
    TextView tvSp;
    @BindView(R.id.tv_jz)
    TextView tvJz;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_statistics;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText("我的爱心荣誉证书");
        statisticsApi();
    }

    @Override
    protected void updateViews() {

    }


    private void statisticsApi() {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("id", UserHelper.getInstance().getloginEntity(StatisticsActivity.this).getId() + "");
        EasyHttp.post(Constants.API_JSONAPI_USERSTATISTICS)
                .params(params)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.e("yang", "统计失败==" + e.toString());
                        MToast.makeTextLong(StatisticsActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String result) {
                        MyLog.e("yang", "统计==" + result);
                        int code = JsonUtil.getJsonValuesInt(result, "code");
                        String msg = JsonUtil.getJsonValuesString(result, "msg");
                        if (code == 0) {
                            Gson gson = new GsonBuilder().create();
                            StatisticsEntity statisticsEntity = gson.fromJson(result, StatisticsEntity.class);
                            tvJyf.setText("总重量"+statisticsEntity.getJyf_sum()+"/次数"+statisticsEntity.getJyf_count());
                            tvDzcp.setText("次数"+statisticsEntity.getDzcp_count());
                            tvSj.setText("总重量"+statisticsEntity.getSj_sum()+"/次数"+statisticsEntity.getSj_count());
                            tvJj.setText("次数"+statisticsEntity.getJj_count());
                            tvLj.setText("总重量"+statisticsEntity.getLj_sum()+"/次数"+statisticsEntity.getLj_count());
                            tvSp.setText("总金额"+String.format("%.2f",statisticsEntity.getSp_sum())+"/订单数"+statisticsEntity.getSp_count());
                            tvJz.setText("总数量"+statisticsEntity.getJz_sum()+"/次数"+statisticsEntity.getJz_count());
                        } else {
                            MToast.makeTextShort(StatisticsActivity.this, msg).show();
                        }

                    }
                });
    }


}
